function [x,fval,gfit]=saeedga(gaeachfitness,nv,gasaset,up,do)

global Disc Mutfact Survrate SelectMethod Ngeneration Npopulation Nselection Tol AcceptedFit LimitRepeat
[Mutfact,Disc,Survrate,SelectMethod,Ngeneration,Npopulation,Nselection,Tol,AcceptedFit,LimitRepeat,OnlyPositive]=gasaset();

limitted=1;
nbit(1:nv)=16;
nb=nv*16;
pop=gafirstpop(Npopulation,nb);
phono=gadecode(pop,nbit,up,do);
[rfit,order]=gapopfitness(gaeachfitness,phono,limitted);
ii=1;
gfit(1)=rfit(1);
fid = fopen('exp.txt', 'wt');
fprintf(fid, '%12.8e\n',rfit(1));
nrepeat=0;

while (ii<Ngeneration)&&(rfit(1)>AcceptedFit)&&(nrepeat<LimitRepeat)
    ii=ii+1;
    npop=ganewpop(pop,order,nb);
    pop=npop;
    phono=gadecode(pop,nbit,up,do);
    [rfit,order]=gapopfitness(gaeachfitness,phono,limitted);
    gfit(ii)=rfit(1);
    fprintf(fid, '%12.8e\n',rfit(1));
    if abs(gfit(ii)-gfit(ii-1))<Tol
        nrepeat=nrepeat+1;
    else
        nrepeat=0;
    end
end

if limitted==0
    for j=1:length(phono(order(1),:))/2
        x(j)=phono(order(1),2*j)*10^phono(order(1),2*j-1);
    end
else
    x=phono(order(1),:);
end

if ii<Ngeneration
    'Optimization terminated due to not effective changes in the fitness function, or achieving accepted fitness'
    nrepeat
    
else
    'optimization continued until limit of number of generations'
end
fval=rfit(1);
fclose(fid)